package com.singerw.service.security;

import com.singerw.pojo.MyUserDetails;
import com.singerw.pojo.TwUser;
import com.singerw.service.TwUserRoleService;
import com.singerw.service.TwUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @version V1.0
 * @Package com.singerw.service.security
 * @auhter 张欣
 * @date 2020/12/29-7:30 PM
 */
@Service
public class MyUserDetailsService implements UserDetailsService {


    @Autowired
    private TwUserService twUserService;

    @Autowired
    private TwUserRoleService twUserRoleService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        TwUser twUser = twUserService.loadUserByUsername(s);
        if (twUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //获取用户角色集
        return new MyUserDetails(twUser,twUserRoleService.findRoleByUserId(twUser.getId()));
    }
}
